<%@ page import="entity.Emp" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.sql.*" %><%--
  Created by IntelliJ IDEA.
  User: Teacher
  Date: 2024/11/5
  Time: 18:13
  To change this template use File | Settings | File Templates.
--%>
<%-- JSP指令 --%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <meta charset="UTF-8">
    <title>员工列表</title>
</head>
<body>
<h1>员工列表</h1>
<a href="">添加员工</a>
<hr>
<%
    Connection conn = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    String url = "jdbc:mysql://127.0.0.1:3306/web?useUnicode=true&characterEncoding=utf-8";
    List<Emp> emps = new ArrayList<>();
    try {
        Class.forName("com.mysql.cj.jdbc.Driver");
        conn = DriverManager.getConnection(url, "root", "root");
        String sql = new StringBuffer()
                .append(" select id,name,salary ")
                .append(" from t_emp ")
                .toString();
        ps = conn.prepareStatement(sql);
        rs = ps.executeQuery();
        while (rs.next()) {
            Emp emp = new Emp();
            emp.setId(rs.getInt("id"));
            emp.setName(rs.getString("name"));
            emp.setSalary(rs.getDouble("salary"));
            emps.add(emp);
        }
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (SQLException e) {
        e.printStackTrace();
    }
%>
<table border="1">
    <tr>
        <th>编号</th>
        <th>姓名</th>
        <th>工资</th>
        <th>操作</th>
    </tr>
    <%
        for(Emp emp : emps){
    %>
    <tr>
        <td><%=emp.getId()%></td>
        <td><%=emp.getName()%></td>
        <td><%=emp.getSalary()%></td>
        <td>
            <a href="">修改</a>
            <a href="">删除</a>
        </td>
    </tr>
    <%
        }
    %>
</table>

</body>
</html>